/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commonsbeans;

import com.sun.faces.context.SessionMap;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Felipe Duarte Clase definida para la navegacion y control de cierre
 * de sesion, y el timeout del sistema se asigno un tiempo de inactividad de 30
 * minutos, despues de este tiempo de inactividad el usuario debe loguearse
 * nuevamente
 */
@ManagedBean
@RequestScoped
public class NavigationBean {

    @ManagedProperty(value = "#{applicationBean}")
    private ApplicationBean applicationBean;
    @ManagedProperty(value = "#{sessionBean}")
    private SessionBean sessionBean;

    /**
     * Creates a new instance of NavigationBean
     */
    public NavigationBean() {
    }

    @PostConstruct
    public void init() {
    }

    //<editor-fold defaultstate="collpased" desc="Getter's and setter's">
    public ApplicationBean getApplicationBean() {
        return applicationBean;
    }

    public void setApplicationBean(ApplicationBean applicationBean) {
        this.applicationBean = applicationBean;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Funcionalidades">
    /**
     * Metodo para el cierre de sesion
     *
     *
     */
    public void cerrarSesion() {

        
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.removeAttribute("sessionBean");
        
        session.removeAttribute("usuario");
        if (session != null) {
            session.invalidate();
            sessionBean.setUsuarioActual(null);
        }
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("login.jsf");
        } catch (IOException ex) {
            Logger.getLogger(NavigationBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Metodo para el timeout
     *
     *
     */
    public void idleAction() {
        if (sessionBean.getUsuarioActual() != null) {
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
            session.removeAttribute("sessionBean");
            if (session != null) {
                session.invalidate();
            }
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("login.jsf");
                applicationBean.setMessage("Su sesión ha sido cerrada por inactividad");
            } catch (IOException ex) {
                Logger.getLogger(NavigationBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
//</editor-fold>
}
